package android.bluebox.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.content.Context;
import android.widget.Toast;

public class StaticBox {

	public static PasswordCrypto passwordCrypto;
	public static KeyCrypto keyCrypto;
	public static PasswordCrypto paircodeCrypto;
	
	public static DatabaseBox dbBox;
	public static NetworkBox netBox;

	public static int currentWorkspaceId = -1;
	public static LocationResult currentLocation = null;
	public static String currentNetwork = null;
	
	
	private static byte[] salt = new byte[] { 0x7d, 0x60, 0x43, 0x5f, 0x02, (byte) 0xe9, (byte) 0xe0, (byte) 0xae };
	private static String sSalt = "wtf";
	
	public static String getCurrentDate() {
		DateFormat dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
		String date = dateFormat.format(new Date());
		return date;
	}
	
	public static long dateToDate(String currentDate, String savedDate) {
		if (savedDate == null || currentDate == null) {
			return 1000000;
		}
		
		if (savedDate.equals("never logged") || currentDate.equals("never logged")) {
			return 1000000;
		}
		
		String[] d1 = currentDate.split(" ")[0].split("/");
		String[] d2 = savedDate.split(" ")[0].split("/");
		
		Calendar cal1 = new GregorianCalendar();
		Calendar cal2 = new GregorianCalendar();
		
		cal1.set(Integer.valueOf(d1[0]), Integer.valueOf(d1[1]), Integer.valueOf(d1[2]));
		cal2.set(Integer.valueOf(d2[0]), Integer.valueOf(d2[1]), Integer.valueOf(d2[2]));
		
		long date = (cal1.getTime().getTime() - cal2.getTime().getTime()) / (1000 * 60 * 60 * 24);
		
		d1 = currentDate.split(" ")[1].split(":");
		d2 = savedDate.split(" ")[1].split(":");
		
		long time = (Integer.parseInt(d1[0]) - Integer.parseInt(d2[0]))*3600 
		+ (Integer.parseInt(d1[1]) - Integer.parseInt(d2[1])) * 60 + (Integer.parseInt(d1[2]) - Integer.parseInt(d1[2]));
		
		return date * 86400 + time;
	}
	
	public static void makeToast(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}
	
	public static String getsSalt() {
		return sSalt;
	}
	
	public static byte[] getSalt() {
		return salt;
	}
	
	public static String getCurrentWorkspaceName() {
		String strHeader = "BLUEBOX";
		if (StaticBox.currentWorkspaceId != -1) {
			String wsName = StaticBox.dbBox.getWorkspace(StaticBox.currentWorkspaceId).getName();
			strHeader += " - " + wsName;
		}
		return strHeader;
	}
	
	public static float distance(LocationResult l1, LocationResult l2) {
		if (l1 != null & l2 != null) {
			
			int R = 6371;
			
			float lat1 = (float) Math.toRadians(l1.getLatitude());
			float lat2 = (float) Math.toRadians(l2.getLatitude());
			float lon1 = (float) Math.toRadians(l1.getLongitude());
			float lon2 = (float) Math.toRadians(l2.getLongitude());
			
			return (float) (Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
	                  Math.cos(lat1)*Math.cos(lat2) * Math.cos(lon2-lon1)) * R);
		} else
			return 1000000;
	}
	
	public static float distance(LocationResult l) {
		return distance(currentLocation, l);
	}
}
